﻿using WAU.Models.BenchmarkModels;
using WAU.Services;
using WAU.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WAU.Models;

namespace WAU.Controllers
{
    /// <summary>
    /// 性能测试控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BenchmarkController : ControllerBase
    {
        private BenchmarkService benchmarkService;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="benchmarkService"></param>
        public BenchmarkController(BenchmarkService benchmarkService)
        {
            this.benchmarkService = benchmarkService;
        }

        /// <summary>
        /// 获取有序 Guid
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string NewSortGuid()
        {
           return GuidFactory.NewSortedGuid(DateTime.Now).ToString();
        }

        /// <summary>
        /// 批量插入 Guid
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public long InsertGuids()
        {
            List<GuidTest> guidTests = new List<GuidTest>();
            for(var i = 0; i < 10000; i++)
            {
                DateTime now = DateTime.Now;
                guidTests.Add(new GuidTest
                {
                    //Guid = Guid.NewGuid().ToString(),
                    Guid = GuidFactory.CreateSortedGuid(now),
                    NowDate = now
                });
            }
            return benchmarkService.InsertGuids(guidTests);
        }

        /// <summary>
        /// 根据有序 Guid 获取时间
        /// </summary>
        /// <param name="sortedGuid"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetSortedGuidTime(string sortedGuid)
        {
            return GuidFactory.GetSortedGuidTime(sortedGuid).ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 获取数据库连接并等待相当长的时间（测试连接池耗尽问题）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public bool GetDbConnectionAndHold()
        {
            benchmarkService.GetDbConnectionAndHold();
            return true;
        }
    }
}
